package com.inspur.edp.formserver.voextendinfo.server.core.rpcserviceimp;

import com.inspur.edp.bef.bizentity.gspbusinessentity.entity.BeConfigCollectionInfo;
import com.inspur.edp.bef.bizentity.gspbusinessentity.entity.GspBeExtendInfo;
import com.inspur.edp.cdp.common.utils.json.JsonUtil;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.GspVoExtendInfo;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.GspVoExtendInfoEntity;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.VoConfigCollectionInfo;
import com.inspur.edp.formserver.vmmanager.extendinfo.repository.GspVoExtendInfoRepository;
import com.inspur.edp.formserver.voextendinfo.server.api.GspVoExtendInfoRpcService;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import com.inspur.edp.lcm.metadata.api.service.MetadataService;
import io.iec.edp.caf.commons.exception.CAFRuntimeException;
import io.iec.edp.caf.commons.exception.ExceptionLevel;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.swagger.util.Json;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class GspVoExtendInfoRpcServiceImp implements GspVoExtendInfoRpcService {
  private static ConcurrentHashMap<String, GspVoExtendInfo> gspVOExtendInfos = new ConcurrentHashMap<>();
  private GspVoExtendInfoRepository voExtendInfoRepository;

  public GspVoExtendInfoRpcServiceImp(GspVoExtendInfoRepository voExtendInfoRepository){
    this.voExtendInfoRepository=voExtendInfoRepository;
  }

  @Override
  public GspVoExtendInfo getVoExtendInfo(String id) {
    if(id == null || id.length() == 0){
      return null;
    }
    if(!gspVOExtendInfos.containsKey(id)) {
      GspVoExtendInfoEntity voExtendInfo = voExtendInfoRepository.findById(id).orElse(null);
      if(voExtendInfo == null)
        return null;
      GspVoExtendInfo gspVoExtendInfo = getGspVoExtendInfo(voExtendInfo);
      gspVOExtendInfos.put(id,gspVoExtendInfo);
    }
      return gspVOExtendInfos.get(id);
  }

  @Override
  public GspVoExtendInfo getVoExtendInfoByConfigId(String configId) {
    if (configId == null || configId.length() == 0) {
      return null;
    }
    if (!gspVOExtendInfos.containsKey(configId)) {
      List<GspVoExtendInfoEntity> infos = voExtendInfoRepository.getVoExtendInfosByConfigId(configId);
      if (infos.size() > 1) {
        StringBuilder exceptionInfo = new StringBuilder("存在重复configID: [" + configId + "],请处理数据库中的异常重复数据。");
        int index = 1;
        for (GspVoExtendInfoEntity info : infos) {
          exceptionInfo.append("\n重复元数据[").append(index).append("]的Id为：[").append(info.getId()).append("]");
          index++;
        }
        throw new CAFRuntimeException("", "", exceptionInfo.toString(), null, ExceptionLevel.Error);
      } else if (infos.size() == 0) {
        return null;
      } else {
        GspVoExtendInfo gspVoExtendInfo = getGspVoExtendInfo(infos.get(0));
        gspVOExtendInfos.put(configId, gspVoExtendInfo);
      }
    }
    return gspVOExtendInfos.get(configId);
  }

  @Override
  public List<GspVoExtendInfo> getVoExtendInfos() {
    List<GspVoExtendInfo> gspVoExtendInfos = new ArrayList<>();
    List<GspVoExtendInfoEntity> gspVoExtendInfoEntities = voExtendInfoRepository.findAll();
    if(gspVoExtendInfoEntities == null || gspVoExtendInfoEntities.size() == 0)
      return gspVoExtendInfos;
    for(GspVoExtendInfoEntity entity: gspVoExtendInfoEntities){
      GspVoExtendInfo gspVoExtendInfo = getGspVoExtendInfo(entity);
      gspVoExtendInfos.add(gspVoExtendInfo);
    }
    return gspVoExtendInfos;
  }

  @Override
  public List<GspVoExtendInfo> getVoId(String beId) {
    List<GspVoExtendInfo> gspVoExtendInfos = new ArrayList<>();
    List<GspVoExtendInfoEntity> gspVoExtendInfoEntities = voExtendInfoRepository.getVoExtendInfoByBeSourceId(beId);
    if(gspVoExtendInfoEntities == null || gspVoExtendInfoEntities.size() == 0)
      return gspVoExtendInfos;
    for(GspVoExtendInfoEntity entity: gspVoExtendInfoEntities){
      GspVoExtendInfo gspVoExtendInfo = getGspVoExtendInfo(entity);
      gspVoExtendInfos.add(gspVoExtendInfo);
    }
    return gspVoExtendInfos;
  }


  public void saveGspVoExtendInfos(List<GspVoExtendInfo> infos){
    if(infos == null || infos.size() == 0)
      return;
    List<GspVoExtendInfoEntity> gspBeExtendInfoEntities = new ArrayList<>();
    for(GspVoExtendInfo info : infos){
      if(info == null)
        continue;
      GspVoExtendInfoEntity entity = getGspVoExtendInfoEntity(info);
      gspBeExtendInfoEntities.add(entity);
    }
    voExtendInfoRepository.saveAll(gspBeExtendInfoEntities);
  }

  public void deleteVoExtendInfo(String id){
    GspVoExtendInfoEntity voExtendInfo = voExtendInfoRepository.findById(id).orElse(null);
    if(id != null && gspVOExtendInfos.containsKey(id)){
      gspVOExtendInfos.remove(id);
    }
    if(voExtendInfo!=null && voExtendInfo.getConfigId()!=null && gspVOExtendInfos.containsKey(voExtendInfo.getConfigId())){
      gspVOExtendInfos.remove(voExtendInfo.getConfigId());
    }
    deleteExtendInfoCache(gspVOExtendInfos,id);
    if(voExtendInfo != null) {
      voExtendInfoRepository.deleteById(id);
    }
  }
  private void deleteExtendInfoCache(ConcurrentHashMap<String, GspVoExtendInfo> infoCache,String infoKey) {
    if (infoCache == null || infoCache.values().size() == 0)
      return;
    String deleteConfigId = null;
    for (GspVoExtendInfo item : infoCache.values()) {
      if (item.getConfigId().equals(infoKey) || item.getId().equals(infoKey)) {
        deleteConfigId = item.getConfigId();
        break;
      }
    }
    if (deleteConfigId != null && !"".equals(deleteConfigId)) {
      infoCache.remove(deleteConfigId);
    }
  }
  private GspVoExtendInfo getGspVoExtendInfo(GspVoExtendInfoEntity gspVoExtendInfoEntity){
    if(gspVoExtendInfoEntity == null)
      return null;
    GspVoExtendInfo gspVoExtendInfo = new GspVoExtendInfo();
    gspVoExtendInfo.setId(gspVoExtendInfoEntity.getId());
    gspVoExtendInfo.setBeSourceId(gspVoExtendInfoEntity.getBeSourceId());
    gspVoExtendInfo.setConfigId(gspVoExtendInfoEntity.getConfigId());
    gspVoExtendInfo.setCreatedBy(gspVoExtendInfoEntity.getCreatedBy());
    gspVoExtendInfo.setCreatedOn(gspVoExtendInfoEntity.getCreatedOn());
    gspVoExtendInfo.setLastChangedBy(gspVoExtendInfoEntity.getLastChangedBy());
    gspVoExtendInfo.setLastChangedOn(gspVoExtendInfoEntity.getLastChangedOn());
    gspVoExtendInfo.setExtendInfo(gspVoExtendInfoEntity.getExtendInfo());
    gspVoExtendInfo.setVoConfigCollectionInfo(convertToVoConfigCollectionInfo(gspVoExtendInfoEntity.getVoConfigCollectionInfo()));
    return gspVoExtendInfo;
  }

  private GspVoExtendInfoEntity getGspVoExtendInfoEntity(GspVoExtendInfo gspVoExtendInfo){
    if(gspVoExtendInfo == null)
      return null;
    GspVoExtendInfoEntity gspVoExtendInfoEntity = new GspVoExtendInfoEntity();
    gspVoExtendInfoEntity.setId(gspVoExtendInfo.getId());
    gspVoExtendInfoEntity.setBeSourceId(gspVoExtendInfo.getBeSourceId());
    gspVoExtendInfoEntity.setConfigId(gspVoExtendInfo.getConfigId());
    gspVoExtendInfoEntity.setCreatedBy(gspVoExtendInfo.getCreatedBy());
    gspVoExtendInfoEntity.setCreatedOn(gspVoExtendInfo.getCreatedOn());
    gspVoExtendInfoEntity.setLastChangedBy(gspVoExtendInfo.getLastChangedBy());
    gspVoExtendInfoEntity.setLastChangedOn(gspVoExtendInfo.getLastChangedOn());
    gspVoExtendInfoEntity.setExtendInfo(gspVoExtendInfo.getExtendInfo());
    gspVoExtendInfoEntity.setVoConfigCollectionInfo(Json.pretty(gspVoExtendInfo.getVoConfigCollectionInfo()));
    return gspVoExtendInfoEntity;
  }


  private VoConfigCollectionInfo convertToVoConfigCollectionInfo(String dbData) {
    if (dbData == null || dbData.isEmpty()) {
      return new VoConfigCollectionInfo();
    }
    VoConfigCollectionInfo configCollectionInfo = null;
    try {
      configCollectionInfo = JsonUtil.toObject(dbData, VoConfigCollectionInfo.class);
    } catch (JsonUtil.JsonParseException e) {
      throw new RuntimeException("反序列化出错", e);
    }
    return configCollectionInfo;
  }
}
